import 'dart:io';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hbtrans_flutter/beans/version_bean.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:package_info/package_info.dart';

import '../apis.dart';
import '../const.dart';
import '../routes.dart';

class Mine extends StatefulWidget {
  @override
  _MineState createState() => _MineState();
}

class _MineState extends State<Mine> {
  var appName, packageName, version = '', buildNumber;
  var loadingDialog;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Future.delayed(Duration(seconds: 0), () {
      PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
        setState(() {
          appName = packageInfo.appName;
          packageName = packageInfo.packageName;
          version = packageInfo.version;
          buildNumber = packageInfo.buildNumber;
        });
      });
    });
  }

  static const jumpPlugin = const MethodChannel('com.jzhu.jump/plugin');

  Future<Null> _toUpdate(String url) async {
    Map<String, String> map = {"url": url};
    String result = await jumpPlugin.invokeMethod('oneAct', map);
    print(result);
  }

  _getById() {
    MyDio.getInstance(url: Const.TRANS_SERVER_MY_PATH)
        .post(Apis.version_getById, (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }
      version_bean versionBean = version_bean.fromJson(data);

      if (versionBean.appVersion != version) {
        var url = versionBean.androidUrl;
        _toUpdate(url);
//        var platform = MethodChannel(Const.CHANNEL);
//        var result = platform.invokeMethod(Const.UPGRADE,{'url':'$url'});

      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Column(
          children: <Widget>[
            SizedBox(
              height: 90,
            ),
            Image.asset(
              'images/icon_logo.png',
              height: 90,
              width: 90,
              fit: BoxFit.fill,
            ),
            SizedBox(
              height: 50,
            ),
            GestureDetector(
              onTap: () {
                print('0');
                String route = '${Routes.personalSet}';
                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_grzx.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '个人中心',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Icon(
                          Icons.chevron_right,
                          color: const Color(0xff23AFDC),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            GestureDetector(
              onTap: () {
                print('0');
                String route = '${Routes.feedBack}';
                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_sugestion.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '意见反馈',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Icon(
                          Icons.chevron_right,
                          color: const Color(0xff23AFDC),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            GestureDetector(
              onTap: () {
                print('0');
                String route = '${Routes.myOrder}';
                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_tiku.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '商城订单',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Icon(
                          Icons.chevron_right,
                          color: const Color(0xff23AFDC),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            GestureDetector(//学车报名订单
              onTap: () {
                print('0');
                String route = '${Routes.signOrder}';
                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_tiku.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '学车报名订单',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Icon(
                          Icons.chevron_right,
                          color: const Color(0xff23AFDC),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            GestureDetector(
              onTap: () {
                String route = '${Routes.aboutUs}';
                Routes.router.navigateTo(context, route,
                    replace: false, transition: TransitionType.fadeIn);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_about_us.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '关于我们',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Icon(
                          Icons.chevron_right,
                          color: const Color(0xff23AFDC),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            GestureDetector(
              onTap: () {
                MyDio.getInstance().clearInstance();
                if (Platform.isIOS) {
                  print('ios');
                  _getById();
                } else if (Platform.isAndroid) {
                  print('android');
                  _getById();
                }
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Image.asset(
                              'images/ic_update_version.png',
                              height: 24,
                              width: 24,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '当前版本',
                              style: TextStyle(
                                  color: const Color(0xff333333), fontSize: 14),
                            )
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Text(
                              version,
                              style: TextStyle(
                                  color: const Color(0xff999999), fontSize: 14),
                            ),
                            Icon(
                              Icons.chevron_right,
                              color: const Color(0xff23AFDC),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
        Container(
          padding: EdgeInsets.only(bottom: 30),
          alignment: Alignment.center,
          child: Text(
            '版权所有：河北省道路运输协会',
            style: TextStyle(fontSize: 12, color: Color(0xff999999)),
          ),
        )
      ],
    );
  }
}
